package com.jvyou.app.ds.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jvyou.app.common.domain.model.PageResult;
import com.jvyou.app.common.utils.orm.WrapperTool;
import com.jvyou.app.ds.domain.dto.CourseSubjectDto;
import com.jvyou.app.ds.domain.entity.CourseSubject;
import com.jvyou.app.ds.mapper.CourseSubjectMapper;
import com.jvyou.app.ds.service.CourseSubjectService;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * 考试科目表(CourseSubject)表服务实现类
 *
 * @author 橘柚
 * @since 2025-04-18 14:48:56
 */
@Service("courseSubjectService")
public class CourseSubjectServiceImpl extends ServiceImpl<CourseSubjectMapper, CourseSubject> implements CourseSubjectService {

    private QueryWrapper<CourseSubject> getWrapper(CourseSubjectDto courseSubjectDto) {
        QueryWrapper<CourseSubject> queryWrapper = new QueryWrapper<>();
        WrapperTool.notEmptyEq(queryWrapper, "subject_code", courseSubjectDto.getSubjectCode());
        WrapperTool.notEmptyEq(queryWrapper, "subject_type", courseSubjectDto.getSubjectType());
        WrapperTool.notEmptyLike(queryWrapper, "subject_name", courseSubjectDto.getSubjectName());
        if (courseSubjectDto.getLicenseTypeId() != null) {
            queryWrapper.inSql("id", "SELECT course_subject_id FROM ds_license_subject WHERE license_type_id='" + courseSubjectDto.getLicenseTypeId() + "'");
        }
        return queryWrapper;
    }

    @Override
    public List<CourseSubject> listCourseSubject(CourseSubjectDto courseSubjectDto) {
        return this.list(getWrapper(courseSubjectDto));
    }

    @Override
    public PageResult<CourseSubject> pageCourseSubject(CourseSubjectDto courseSubjectDto) {
        IPage<CourseSubject> page = new Page<>(courseSubjectDto.getCurrent(), courseSubjectDto.getPageSize());
        page = page(page, getWrapper(courseSubjectDto));
        return PageResult.success(page);
    }
}
